package com.service;/**
 * @program: SmbmsDemo
 * @description: 供应商实现层
 * @author: Mr.Kang
 * @create: 2020-06-01 12:03
 **/

import com.mapper.BillDao;
import com.mapper.ProviderDao;
import com.pojo.Provider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.util.List;

/**
 * @Description:
 * @author 康康真滴暖
 * @date 2020-06-01 12:03
 */
@Service
public class ProviderServiceImpl implements ProviderService {
    private Logger log= LogManager.getLogger(UserServiceImpl.class);
    @Resource
    private ProviderDao providerDao;
    @Resource
    private BillDao billDao;

    @Override
    public int add(Provider provider) throws Exception {
           int count= providerDao.add(provider);
            if(count> 0){
                log.info("新增成功");
            }else {
                log.info("新增失败");
            }
        return count;
    }

    @Override
    public List<Provider> getProviderList(String proName, String proCode) throws Exception {

        List<Provider> providerList = null;
        try {
            providerList = providerDao.getProviderList( proName,proCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return providerList;
    }

    @Override
    public int deleteProviderById(String delId) throws Exception {
        int billCount = -1;
        try {
            billCount = billDao.getBillCountByProviderId(delId);
            if(billCount == 0){
                providerDao.deleteProviderById(delId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            billCount = -1;
        }
        return billCount;
    }

    @Override
    public Provider getProviderById(String id) throws Exception {
        Provider provider = null;
        try{
            provider = providerDao.getProviderById(id);
        }catch (Exception e) {
            e.printStackTrace();
            provider = null;
        }
        return provider;
    }

    @Override
    public int modify(Provider provider) throws Exception {
      int count=providerDao.modify(provider);
        try {
            if(count > 0){
                log.info("修改成功");
            }else {
                log.info("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }
}
